package ExampleDemo;

import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;
import javax.swing.table.TableModel; 
import java.awt.event.*;

public class GradeExaple extends  JFrame implements ChangeListener{
 
JPanel p1,p2,p3;
JTabbedPane tab;
JButton confirm;
int count=0;
    JTextArea ta=new JTextArea("学号成绩",30,20);
ArrayList array=new ArrayList();
JTextField sno=new JTextField(9);
 JTextField grade=new JTextField(3);
  
  
 //将学号和成绩封装 成一information类
    class information
{
 
    public String no;
    public String Grade;
    public information(String s,String g){
 
    no=s;
    Grade=g;
     
     
}
     
     
     
}
 
 
//排序成绩
public class Mycomparator implements Comparator{
 
public int compare(Object o1,Object o2) {
 
information f1=(information)o1;
information f2=(information)o2;
int n=Integer.parseInt(f1.Grade);
        int m=Integer.parseInt(f2.Grade);
        if(n<m)
        return 1;
        else return 0;
 
 
}
 
}
 
 
public class sortListener implements ActionListener{
 
public void actionPerformed(ActionEvent e){
 
Comparator comp = new Mycomparator();
Collections.sort(array,comp); 
String s5="学号      成绩\n";
for(int i=0;i<array.size();i++){
 
s5+=((information)array.get(i)).no +"      "+((information)array.get(i)).Grade+"\n";
 
}
ta.setText(s5);
 
 
}
 
}
 
     
 
 
 
public GradeExaple(){
 
JFrame f=new JFrame("JTabbedPane_ex");
setSize(50,50);
setVisible(true);
this.setLayout(new FlowLayout());
p1=new JPanel();
p1.setLayout(new FlowLayout());
 
 
 
 
     
    class ButtonListener implements ActionListener{
 
public void actionPerformed(ActionEvent e){
 
String s=sno.getText();
String s1=grade.getText();
 
    final information info=new information(s,s1);
array.add(info);
count++;
 
 
}
 
};
     
confirm=new JButton("确认");
confirm.addActionListener(new ButtonListener());
p1.add(new JLabel("学号"));
p1.add(sno);
p1.add(new JLabel("成绩"));
p1.add(grade);
p1.add(confirm);
 
 
 
 
 
 
p2=new JPanel();
p3=new JPanel();
tab=new JTabbedPane();
tab.setTabPlacement(JTabbedPane.TOP);
 
tab.addTab("成绩输入",p1);
tab.addTab("成绩查询",p2);
tab.addTab("成绩排序",p3);
add(tab);
 
f.getContentPane().add(tab);
f.pack();
f.setVisible(true);
 
//成绩查询
 
JButton b1=new JButton("查询");
JLabel lb1=new JLabel("请输入学号");
final JTextField num=new JTextField(9);
    JLabel lb2=new JLabel("成绩为");
    final JTextField lb3=new JTextField(3);
    lb3.setVisible(true);
    p2.add(lb1);
    p2.add(num);
    p2.add(b1);
    p2.add(lb2);
    p2.add(lb3);
     
    class jbListener implements ActionListener{
 
    public void actionPerformed(ActionEvent e){
 
    for(int i=0;i<count;i++){
 
    if(((information)array.get(i)).no.equalsIgnoreCase(num.getText()))
    {
 
    lb3.setText(((information)array.get(i)).Grade);
     
}
     
}
     
     
}
     
     
}
    b1.addActionListener(new jbListener());
       
    JButton sort=new JButton("排序"); 
    p3.add(sort);
    JScrollPane scrollpaneTa=new JScrollPane();
    p3.add(scrollpaneTa);
    p3.add(ta);
    sort.addActionListener(new sortListener());
 
}     

    public void stateChanged(ChangeEvent e){

}

public static void main(String[]  args){
 
JTabbedPane e=new JTabbedPane();
 
 
}
 
 
}